import is.glif.fenius.ws.simple.SimpleFeniusService;
import is.glif.fenius.ws.simple.Simple;

import is.glif.fenius.ws.simple.ReservationParams;
import is.glif.fenius.ws.simple.CreateReq;
import is.glif.fenius.ws.simple.CreateResp;
import is.glif.fenius.ws.simple.ListReq;
import is.glif.fenius.ws.simple.ListResp;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeFactory;

import java.util.List;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class FeniusSimpleClient {
    static boolean debug = true;
    static String APOINT = "g-lambda.net:TOKYO";
    static String ZPOINT = "g-lambda.net:TSUKUBA";

    private SimpleFeniusService tl = null;
    Simple service;
    SimpleFeniusService port;

    public FeniusSimpleClient() 
    {
	service = new Simple();
        port = service.getSimpleFeniusServicePort();
    }

    public synchronized void doCreate
	(String id, Calendar startTime, int duration,
	 int bandwidth, String apoint, String zpoint) 
    {
	if (port == null) {
	    System.err.println("FeniusClinet:port is null in doCreate().");
	    return;
	}

	try {
	    ReservationParams resvParams = new ReservationParams();
	    resvParams.setReservationId(id);

	    DatatypeFactory df= DatatypeFactory.newInstance();
	    XMLGregorianCalendar gc = df.newXMLGregorianCalendar
		(startTime.get(Calendar.YEAR),
		 startTime.get(Calendar.MONTH) + 1,
		 startTime.get(Calendar.DAY_OF_MONTH),
		 startTime.get(Calendar.HOUR_OF_DAY),
		 startTime.get(Calendar.MINUTE),
		 startTime.get(Calendar.SECOND),
		 0, 0);
	    // System.err.println("gc=" + gc);
	    resvParams.setActivationTime(gc);
	
	    resvParams.setDuration(new Integer(duration));
	    resvParams.setBandwidth(new Integer(bandwidth));
	    resvParams.setSrcEndpoint(apoint);
	    resvParams.setDstEndpoint(zpoint);
	    resvParams.setStatus("START");
	    
	    if (debug) printParams("Create request", resvParams);
	    CreateReq req = new CreateReq();
	    req.setResvParams(resvParams);

	    CreateResp res = port.create(req);

	    ReservationParams rp = res.getResvParams();
	    if (debug) printParams("Create response", rp);
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
    }

    public synchronized List<ReservationParams> doList() 
    {
	if (port == null) {
	    System.err.println("FeniusClinet:port is null in doList().");
	    return null;
	}

	try {
	    ListReq req = new ListReq();
	    ListResp result = port.list(req);
	    
	    if (result == null) return null;
	    List<ReservationParams> rps = result.getReservations();

	    if (debug) {
		int size = rps.size();
		ReservationParams[] rp = new ReservationParams[size];
		ReservationParams[] rpp = rps.toArray(rp);
		for (int i = 0; i < size; i++) {
		    printParams("Create response", rpp[i]);
		}
	    }
	    return rps;
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
	return null;
    }

    public static void printParams(String m, ReservationParams p)
    {
	String s = m + ":\n";
	if (p != null) {
	    m += "\t" + p.getReservationId() 
		+ "::" + p.getSrcEndpoint() 
		+ "->" + p.getDstEndpoint()  
		+ ":" + p.getStatus();
	    
	    XMLGregorianCalendar c = p.getActivationTime();
	    m += ", Time=" + c.getYear() + "/" + c.getMonth() 
		+ "/" + c.getDay();
	    m += " " + c.getHour() + ":" + c.getMinute() + ":" + c.getSecond();
	    m += ", Duration=" + p.getDuration() + ", BW=" + p.getBandwidth();
	}
	System.out.println(m);
    }


    public static void main(String[] args)
    {
	TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
	FeniusSimpleClient fc = new FeniusSimpleClient();

	Calendar startTime = Calendar.getInstance();
	startTime.setTime(new Date());
	startTime.set(Calendar.MILLISECOND, 0);
	startTime.add(Calendar.MINUTE, 1);

	fc.doCreate("FeniusSimpleClient", startTime, 120, 
		    2000 * 1000000, APOINT, ZPOINT);

	int i = 15;
	while (i-- > 0) {
	    try {
		List<ReservationParams> l = fc.doList();
		Thread.sleep(10 * 1000);
	    } catch (InterruptedException e) {
	    }
	}
    }
}